package com.thfw.test.view.fragment;

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.thfw.test.MyApplication;
import com.thfw.test.utils.ConfigurationService;

import javax.inject.Inject;

import butterknife.ButterKnife;

/**
 * Created by scooper_lxx on 2017/5/26.
 */

public abstract class BaseFragment extends Fragment {

    protected View rootView;
    protected Activity mContext;
    protected boolean hasCreated;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mContext = getActivity();
//        hasCreated = true;
        firstCreate();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if(rootView == null){
            rootView = inflater.inflate(getLayoutResource(),container,false);
        }
        ButterKnife.bind(this,rootView);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();

    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        hasCreated = false;
    }

    @Override
    public void onDestroy() {
        rootView = null;
        mContext = null;
//        hasCreated = false;
        super.onDestroy();
    }

    //获取布局文件
    protected abstract int getLayoutResource();
    //初始化view
    protected abstract void initView();


    protected abstract void firstCreate();
    /**
     * 直接跳转不带bound
     * @param cls
     */
    public void startActivity(Class<?> cls){
        startActivity(cls , null);
    }

    /**
     * 通过Class跳转界面
     * @param cls
     * @param requestCode
     */
    public void startActivityForResult(Class<?> cls ,int requestCode){
        startActivityForResult(cls,null,requestCode);
    }

    /**
     * 含有bundle跳转界面
     * @param cls
     * @param bundle
     * @param requestCode
     */
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(getActivity(),cls);
        if(bundle != null){
            intent.putExtras(bundle);
        }
        startActivityForResult(intent,requestCode);
    }

    /**
     * 含有bundle跳转界面
     * @param cls
     * @param bundle
     */
    public void startActivity(Class<?> cls ,Bundle bundle){
        Intent intent = new Intent();
        intent.setClass(getActivity(),cls);
        if(bundle != null){
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    public ConfigurationService getConfig(){
        return ((MyApplication)(getActivity().getApplication())).getConfigurationService();
    }
}
